package metier.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;

import configuration.MessageAppli;

public class CommunJdbc {

	/**
	 * @param c
	 * @param table
	 * @return Retourne l'id max.
	 */
	public static int getIdMaxTable(Connection c, String table) {
		int i = 0;
		try {
			Statement st = c.createStatement();
			ResultSet res = st.executeQuery("SELECT MAX(id) FROM " +  table);
			while(res.next()){
				i = res.getInt(1);
			}
			st.close();
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, MessageAppli.MSG_BDD_ERREUR_REQUETE,
					MessageAppli.MSG_BDD_TITRE_ERREUR, JOptionPane.ERROR_MESSAGE);
		}
		return i;
	}
	
	/**
	 * @param c
	 * @param table
	 * @return Retourne l'id suivant pour insertion.
	 */
	public static int getProchainIdTable(Connection c, String table) {
		return getIdMaxTable(c, table) + 1;
	}
}
